const app = new Vue({
    el: '#app',
    data: {
      isNull: true,
      books:[
        {
          id: 1,
          bookName:'《Java编程思想》',
          publishDate: '2020-09',
          bookPrice: 98.00,
          purchaseQuantity: 1
        },
        {
          id: 2,
          bookName:'《数据分析与数据原理》',
          publishDate: '2019-02',
          bookPrice: 39.00,
          purchaseQuantity:1
        },
        {
          id: 3,
          bookName:'《Hadoop权威指南》',
          publishDate: '2019-10',
          bookPrice: 59.00,
          purchaseQuantity:1
        },
        {
          id: 4,
          bookName:'《代码大全》',
          publishDate: '2018-08',
          bookPrice: 128.00,
          purchaseQuantity:1
        }
      ]
    },
    /* 过滤器 */
    filters: {
      showPrice(bookPrice){
        return  '￥'+ bookPrice.toFixed(2)
      }
    },
    computed:{
      totalPrice:{
        get() {
       /*   let totalPrice = 0;
          for (let book of this.books){
            totalPrice +=book.purchaseQuantity * book.bookPrice
          }
          return totalPrice*/
       let totalPrice = this.books.reduce((pre,book) => pre + book.bookPrice * book.purchaseQuantity,0)
          return totalPrice
        }
      }
    },
    methods:{
      increase(indexBooks){
          // console.log(this.books[indexBooks].purchaseQuantity);
          this.books[indexBooks].purchaseQuantity++
      },
      decrease(indexBooks) {
          if (this.books[indexBooks].purchaseQuantity > 0) {
            return this.books[indexBooks].purchaseQuantity--
          } else {
            return this.books[indexBooks].purchaseQuantity = 0
          }
      },
      remove(indexBooks){
          this.books.splice(indexBooks,1)
          if(this.books == null || this.books == ''){
            return this.isNull = !this.isNull
          }
      },
    }
  })